#-*- coding:utf-8 -*-
# Copyright 2017 Xiaomi, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import os
base_dir = os.path.dirname(os.path.abspath(__file__))
# activate_this = '%s/env/bin/activate_this.py' % base_dir
# execfile(activate_this, dict(__file__=activate_this))

import sys
sys.path.insert(0, base_dir)
import logging

# open-falcon --- app
from rrd import app as falcon_app

# tj -- app
from monitor_bk_site import create_app
monitor_app = create_app(os.environ.get("FLASK_CONFIG") or "default")

from werkzeug.wsgi import DispatcherMiddleware
from werkzeug.serving import run_simple
app = DispatcherMiddleware(falcon_app, {'/test': monitor_app})

# log setting
from settings import logfile, logdir
if not os.path.exists(logdir):
    os.makedirs(os.path.join(logdir,logfile))
handler = logging.FileHandler(os.path.join(logdir,logfile), encoding='UTF-8')
handler.setLevel(logging.DEBUG)
logging_format = logging.Formatter(
    '%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')
handler.setFormatter(logging_format)
monitor_app.logger.addHandler(handler)

if __name__ == "__main__":
    run_simple('0.0.0.0', 8000, app, use_reloader=True, use_debugger=True)
    # app.run(host="0.0.0.0", port=8081, debug=True)
